// pages/orderDetails/orderDetails.js
const app = getApp()
var timeChange = require('../../utils/timeChange.js');
var timeType = '';
// 倒计时总时间
var totalTime = 0;
var typeFlag = 99;
var cutDownTime = '';
// 按钮的状态(取消订单: 0,去支付: 1,申请退款: 2,去评价: 3,查看退款详情: 4)
var btnState = 999
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bgImage: '/images/orderDetails_wave.png',
    stateIconImage: '',
    arrowImage: '/images/order_arrow.png',
    iconBgImage: '/images/order_more.png',
    soiModel: [],
    createTime: "",
    // 订单的描述
    finishT: "",
    // 订单状态的描述
    stateT: "",
    // 开始时间
    startTime: "",
    // 结束时间
    endTIME: "",
    endTime: "",
    // 去支付倒计时时间
    modifyTime: "",
    // 是否显示退款详情条目
    isRefundVHidden: true,
    // 退款详情条目的内容
    refundT: '',
    // 判断退款记录的参数(有一条或多条)
    refundStatus: 999,
    // 有倒计时下面的view显示
    isTimeVHidden: true,
    // 有倒计时 背景显示
    isTimeBgVHidden: true,
    // 有倒计时描述
    timeT: '',
    // 退款箭头隐藏
    arrowImageHidden: false,
    // 退款详情下的背景条目
    bgVHidden: false,
    // 联系房东是否隐藏
    isContactLandlordBtnHidden: false,
    // 房东手机号
    landPhone: 0,
    // 租户协议的天数
    refundAgreement: '',
    // 房间套数
    reserveNumber: 0,
    // 房间id
    sriRid: 0,
    // 退款倒计时时间
    refundTime: '',
    // 订单单号
    soiId: 0,
    // 倒计时
    timer: '',
    // 退款单号
    srId: 0,
    // 右侧按钮的title
    rightBtnT: '',
    // 右侧按钮是否隐藏
    rightBtnHidden: true,
    // 左侧按钮是否隐藏
    leftBtnHidden: true,
    // 右侧按钮的颜色
    rightBorderColor: 0,
    // 评价的星星
    starImage: '/images/grayStar.png',
    selectedStarImage: '/images/star.png',
    // 评价是否隐藏
    isCouponsHidden: true,
    // 评价的分数
    score: 0,
    // 评价的内容
    couponsContent: '',
    // 评价的时间
    couponsTime: '',
    // 评价的图片数组
    couponsImageList: [],
    starArr: [1,2,3,4,5],
  },
  // 加载数据
  loadData(username, solid) {
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })
    wx.request({
      url: app.baseNet + 'orderInfo/findOrderInfo?',
      data: {
        soiId: solid,
        userName: username
      },
      method: 'GET',
      header: {
        'Content-Type': 'application/json'
      },
      success(res) {
        console.log(res.data)
        wx.hideLoading()
        if (res.data.messcode == '001') {
          var time = timeChange.formatTimeTwo(res.data.soi.createTime, 'Y-M-D h:m:s')
          that.setData({
            soiModel: res.data.soi,
            createTime: time,
            modifyTime: res.data.soi.modifyTime,
            startTime: res.data.soi.startTime,
            endTIME: res.data.soi.endTime,
            endTime: res.data.soi.createTime,
            refundStatus: res.data.soi.refundStatus || 0,
            landPhone: res.data.soi.landPhone,
            refundAgreement: res.data.soi.refundAgreement || '',
            sriRid: res.data.soi.sriRid,
            reserveNumber: res.data.soi.reserveNumber,
            refundTime: res.data.soi.refundTime,
            soiId: res.data.soi.soiId,
            srId: res.data.soi.srId,
          })
          if (res.data.roomEvaluateDto != null) {
            that.setData({
              score: res.data.roomEvaluateDto.user.score || 0,
              couponsContent: res.data.roomEvaluateDto.user.content || '',
              couponsTime: res.data.roomEvaluateDto.user.createTime || '',
              couponsImageList: res.data.roomEvaluateDto.roomEvaluatePictureList || [],
            })
          }
          that.judgeState(res.data.soi.state)
        } else if (res.data.messcode == '002') {
          // 没有数据
        } else {
          // 服务器异常
        }
      },
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var phoneNumber = wx.getStorageSync('phoneNumber')
    console.log(options.soiId)
    this.setData({
      soiId: options.soiId,
    })
    this.loadData(phoneNumber, this.data.soiId)
  },

  judgeState(state) {
    switch (state) {
      case 0:
        this.setData({
          stateT: '待确认',
          stateIconImage: '/images/order_succeed.png',
          isRefundVHidden: true,
          timeT: '房东正在查看您的订单，接受后您即可付款入住，请耐心等待。',
          bgVHidden: false,
          isContactLandlordBtnHidden: true,
          isTimeVHidden: false,
          rightBtnHidden: false,
          rightBtnT: '取消订单',
          rightBorderColor: 1,
        })
        btnState = 0
        totalTime = 60*60*2
        timeType = "0:0:0"
        typeFlag = 1
        cutDownTime = this.data.endTime
        this.countdown(this.data.endTime, 60*60*2, "0:0:0", 1)
        break;
      case 1:
        this.setData({
          stateT: '待付款',
          stateIconImage: '/images/order_succeed.png',
          arrowImageHidden: true,
          isRefundVHidden: true,
          timeT: '房东确认已通过，您的预订还未完成，请尽快完成支付。',
          bgVHidden: false,
          isContactLandlordBtnHidden: true,
          isTimeVHidden: false,
          leftBtnHidden: false,
          rightBtnHidden: false,
          rightBtnT: '去支付',
          rightBorderColor: 2,
        })
        btnState = 1
        totalTime = 60 * 30
        timeType = "0:0"
        typeFlag = 0
        cutDownTime = this.data.modifyTime
        this.countdown(this.data.modifyTime, 60 * 30, "0:0", 0)
        break;
      case 2:
        this.judgeWaitCheckIn()
        if (this.data.refundStatus == 1 || this.data.refundStatus == 2) {
          this.setData({
            stateT: '待入住',
            stateIconImage: '/images/order_succeed.png',
            finishT: '您的预订已成功，等待入住',
            isRefundVHidden: false,
            refundT: '您有退款申请记录，点击查看详情',
            bgVHidden: false,
            isContactLandlordBtnHidden: false,
            rightBtnT: '申请退款',
            rightBorderColor: 2,
          })
        } else {
          this.setData({
            stateT: '待入住',
            stateIconImage: '/images/order_succeed.png',
            finishT: '您的预订已成功，等待入住',
            isRefundVHidden: true,
            bgVHidden: true,
            isContactLandlordBtnHidden: false,
            rightBtnT: '申请退款',
            rightBorderColor: 2,
          })
        }
        btnState = 2
        break;
      case 3:
        if (this.data.refundStatus == 1 || this.data.refundStatus == 2) {
          this.setData({
            stateT: '已入住',
            stateIconImage: '/images/order_succeed.png',
            finishT: '您已办理入住',
            isRefundVHidden: false,
            refundT: '您有退款申请记录，点击查看详情',
            bgVHidden: false,
            isContactLandlordBtnHidden: false,
          })
        } else {
          this.setData({
            stateT: '已入住',
            stateIconImage: '/images/order_succeed.png',
            finishT: '您已办理入住',
            isRefundVHidden: true,
            bgVHidden: true,
            isContactLandlordBtnHidden: false,
          })
        }
        break;
      case 4:
        if (this.data.refundStatus == 1 || this.data.refundStatus == 2) {
          this.setData({
            stateT: '已完成',
            stateIconImage: '/images/order_succeed.png',
            finishT: '您已完成入住，请您给出诚恳评价',
            isRefundVHidden: false,
            refundT: '您有退款申请记录，点击查看详情',
            bgVHidden: false,
            isContactLandlordBtnHidden: false,
            rightBtnHidden: false,
            rightBtnT: '去评价',
            rightBorderColor: 2,
          })
        } else {
          this.setData({
            stateT: '已完成',
            stateIconImage: '/images/order_succeed.png',
            finishT: '您已完成入住，请您给出诚恳评价',
            isRefundVHidden: true,
            bgVHidden: true,
            isContactLandlordBtnHidden: false,
            rightBtnHidden: false,
            rightBtnT: '去评价',
            rightBorderColor: 2,
          })
        }
        btnState = 3
        break;
      case 5:
        if (this.data.refundStatus == 1 || this.data.refundStatus == 2) {
          this.setData({
            stateT: '已完成',
            stateIconImage: '/images/order_succeed.png',
            finishT: '订单已完成。',
            isRefundVHidden: false,
            refundT: '您有退款申请记录，点击查看详情',
            bgVHidden: false,
            isContactLandlordBtnHidden: false,
            isCouponsHidden: false,
          })
        } else {
          this.setData({
            stateT: '已完成',
            stateIconImage: '/images/order_succeed.png',
            finishT: '订单已完成。',
            isRefundVHidden: true,
            bgVHidden: true,
            isContactLandlordBtnHidden: false,
            isCouponsHidden: false,
          })
        }
        break;
      case 6:
        this.setData({
          stateT: '已取消',
          stateIconImage: '/images/order_loseEfficacy.png',
          finishT: '您已取消订单。',
          isRefundVHidden: true,
          bgVHidden: true,
          isContactLandlordBtnHidden: true,
        })
        break;
      case 7:
        this.setData({
          stateT: '已失效',
          stateIconImage: '/images/order_loseEfficacy.png',
          finishT: '房东已取消该订单',
          isRefundVHidden: true,
          bgVHidden: true,
          isContactLandlordBtnHidden: true,
        })
        break;
      case 8:
        this.setData({
          stateT: '待退款',
          stateIconImage: '/images/order_clock.png',
          finishT: '',
          isRefundVHidden: false,
          refundT: '您有退款申请记录，点击查看详情',
          bgVHidden: true,
          isContactLandlordBtnHidden: false,
          isTimeVHidden: false,
          timeT: '您已申请退款，等待审核中',
          isTimeBgVHidden: false,
        })
        totalTime = 60 * 60 * 24 * 3
        timeType = " 0天0:0:0"
        typeFlag = 2
        cutDownTime = this.data.refundTime
        this.countdown(this.data.refundTime, 60 * 60 * 24 * 3, " 0天0:0:0", 2)
        break;
      case 9:
        if (this.data.refundStatus == 1 || this.data.refundStatus == 2) {
          this.setData({
            stateT: '退款中',
            stateIconImage: '/images/order_clock.png',
            finishT: '退款申请已通过，等待退款中',
            isRefundVHidden: false,
            refundT: '您有退款申请记录，点击查看详情',
            bgVHidden: false,
            isContactLandlordBtnHidden: false,
          })
        } else {
          this.setData({
            stateT: '退款中',
            stateIconImage: '/images/order_clock.png',
            finishT: '退款申请已通过，等待退款中',
            isRefundVHidden: true,
            bgVHidden: true,
            isContactLandlordBtnHidden: false,
          })
        }
        break;
      case 10:
        this.setData({
          stateT: '已失效',
          stateIconImage: '/images/order_loseEfficacy.png',
          finishT: '对不起，房东待确认超时，订单已失效。',
          isRefundVHidden: true,
          bgVHidden: true,
          isContactLandlordBtnHidden: true,
        })
        break;
      case 11:
        if (this.data.refundStatus == 1 || this.data.refundStatus == 2) {
          this.setData({
            stateT: '已关闭',
            stateIconImage: '/images/order_loseEfficacy.png',
            finishT: '已退款，订单自动关闭',
            isRefundVHidden: false,
            refundT: '您有退款申请记录，点击查看详情',
            bgVHidden: false,
            isContactLandlordBtnHidden: false,
          })
        } else {
          this.setData({
            stateT: '已关闭',
            stateIconImage: '/images/order_loseEfficacy.png',
            finishT: '已退款，订单自动关闭',
            isRefundVHidden: true,
            bgVHidden: true,
            isContactLandlordBtnHidden: false,
          })
        }
        break;
      case 12:
        this.setData({
          stateT: '已失效',
          stateIconImage: '/images/order_loseEfficacy.png',
          finishT: '订单已失效',
          isRefundVHidden: true,
          bgVHidden: true,
          isContactLandlordBtnHidden: true,
        })
        break;
      case 13:
        if (this.data.refundStatus == 1 || this.data.refundStatus == 2) {
          this.judgeWaitCheckIn()
          this.setData({
            stateT: '待入住',
            stateIconImage: '/images/order_loseEfficacy.png',
            finishT: '退款申请审核失败',
            isRefundVHidden: false,
            refundT: '您有退款申请记录，点击查看详情',
            bgVHidden: false,
            isContactLandlordBtnHidden: false,
          })
        } else {
          this.setData({
            stateT: '待入住',
            stateIconImage: '/images/order_loseEfficacy.png',
            finishT: '退款申请审核失败',
            isRefundVHidden: true,
            bgVHidden: true,
          })
        }
        btnState = 2
        break;
    }
  },
  // 右侧按钮的点击事件
  rightBtnClick: function (e) {
    var that = this
    switch (btnState) {
      case 0:
        // 取消订单
        wx.navigateTo({
          url: '../cancelOrder/cancelOrder?soiId=' + that.data.soiModel.soiId + '&orderNum=' + 1,
        })
        break;
      case 1:
        // 去支付
        wx.navigateTo({
          url: '../toPayfor/toPayfor?soiId=' + that.data.soiModel.soiId + '&orderNum=' + 1,
        })
        break;
      case 2:
        // 申请退款
        wx.navigateTo({
          url: '../applyForRefund/applyForRefund?soiId=' + that.data.soiModel.soiId + '&actualPrice=' + that.data.soiModel.actualPrice + '&orderNum=' + 1,
        })
        break;
      case 3:
        // 去评价
        wx.navigateTo({
          url: '../goEvaluate/goEvaluate?iconImage=' + that.data.soiModel.picSrc + '&soiRid=' + that.data.soiModel.soiId + '&sriRid=' + that.data.soiModel.sriRid + '&orderNum=' + 1,
        })
        break;
      case 4:
        // 查看退款详情
        if (model.refundStatus == 1) {
          wx.navigateTo({
            url: '../refundDetail/refundDetail?srId=" + that.data.soiModel.srId',
          })
        } else {
          wx.navigateTo({
            url: '../refundList/refundList?orderNumber=" + model.data.soiId + "&refundStatus=" + that.data.soiModel.refundStatus',
          })
        }
        break;
    }
  },
  // 左侧按钮的点击事件
  leftBtnClick: function (e) {
    // 取消订单
    var that = this;
    wx.navigateTo({
      url: '../cancelOrder/cancelOrder?soiId=' + that.data.soiModel.soiId + '&orderNum=' + 1,
    })
  },
  // 倒计时
  countdown(endTime, totalTime, timeType, timeFlag) {
    var that = this;
    var EndTime = endTime || [];
    var NowTime = Date.parse(new Date()) / 1000;
    var total_micro_second = EndTime + totalTime - NowTime || [];
    console.log('剩余时间：' + total_micro_second);
    var cutdownT = that.dateformat(total_micro_second, timeFlag)
    console.log(cutdownT)
    // 渲染倒计时时钟
    that.setData({
      finishT: cutdownT
    });
    if(total_micro_second <= 0) {
      that.setData({
        finishT: timeType
      });
      return;
    }
    that.setData({
      timer: setTimeout(function () {
        total_micro_second -= 1;
        that.countdown(cutDownTime, totalTime, timeType, timeFlag);
      }
        , 1000)
    })
 },
 // 点击查看退款详情
  searchRefundDetailClick: function() {
    var that = this
    if (that.data.refundStatus == 1) {
      wx.navigateTo({
        url: '../refundDetail/refundDetail?srId=' + that.data.srId,
      })
    } else if (that.data.refundStatus == 2) {
      wx.navigateTo({
        url: '../refundList/refundList?orderNumber=' + that.data.soiId + '&refundStatus=' + that.data.refundStatus,
      })
    }
  },
// 时间格式化输出，如11:03 25:19 每1s都会调用一次
  dateformat(micro_second, timeFlag) {
  // 总秒数
  var second = Math.floor(micro_second);
  // 天数
  var day = Math.floor(second / 3600 / 24);
  // 小时
  var hr = Math.floor(second / 3600 % 24);
  // 分钟
  var min = Math.floor(second / 60 % 60);
  // 秒
  var sec = Math.floor(second % 60);
  if (timeFlag == 1) {
    return hr + ":" + min + ":" + sec;
  } else if (timeFlag == 2) {
    return day + "天" + hr + ":" + min + ":" + sec;
  } else {
    return min + ":" + sec;
  }
},
// 联系房东的点击事件
  contactLandlordBtnClick: function() {
    var that = this;
    wx.makePhoneCall({
      phoneNumber: that.data.landPhone,
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  // 点击查看明细
  searchDetailsBtnClick: function() {
    var that = this;
    wx.navigateTo({
      url: '../order_searchDetail/order_searchDetail?sriRid=' + that.data.sriRid + '&startTime=' + that.data.startTime + '&endTime=' + that.data.endTIME + '&reserveNumber=' + that.data.reserveNumber + '&soiId=' + that.data.soiModel.soiId,
    })
  },
  // 判断待入住是否有申请退款
  judgeWaitCheckIn() {
    var that = this; 
    var timestamp = Date.parse(new Date(that.data.startTime));
    timestamp = timestamp / 1000;
    var nowTime = Date.parse(new Date()) / 1000;
    console.log(timestamp)
    console.log(nowTime)
    if (that.data.soiModel.refundAgreement == '当天') {
      if (nowTime > timestamp) {
        that.setData({
          rightBtnHidden: true,
        })
      } else {
        that.setData({
          rightBtnHidden: false,
        })
      }
    } else if (that.data.soiModel.refundAgreement == '前一天') {
      if (nowTime > timestamp - 60 * 60 * 24) {
        that.setData({
          rightBtnHidden: true,
        })
      } else {
        that.setData({
          rightBtnHidden: false,
        })
      }
    } else if (that.data.soiModel.refundAgreement == '前两天') {
      if (nowTime > timestamp - 60 * 60 * 24 * 2) {
        that.setData({
          rightBtnHidden: true,
        })
      } else {
        that.setData({
          rightBtnHidden: false,
        })
      }
    } else if (that.data.soiModel.refundAgreement == '前三天') {
      if (nowTime > timestamp - 60 * 60 * 24 * 3) {
        that.setData({
          rightBtnHidden: true,
        })
      } else {
        that.setData({
          rightBtnHidden: false,
        })
      }
    } else if (that.data.soiModel.refundAgreement == '前四天') {
      if (nowTime > timestamp - 60 * 60 * 24 * 4) {
        that.setData({
          rightBtnHidden: true,
        })
      } else {
        that.setData({
          rightBtnHidden: false,
        })
      }
    } else if (that.data.soiModel.refundAgreement == '前五天') {
      if (nowTime > timestamp - 60 * 60 * 24 * 5) {
        that.setData({
          rightBtnHidden: true,
        })
      } else {
        that.setData({
          rightBtnHidden: false,
        })
      }
    } else if (that.data.soiModel.refundAgreement == '前六天') {
      if (nowTime > timestamp - 60 * 60 * 24 * 6) {
        that.setData({
          rightBtnHidden: true,
        })
      } else {
        that.setData({
          rightBtnHidden: false,
        })
      }
    } else if (that.data.soiModel.refundAgreement == '前七天') {
      if (nowTime > timestamp - 60 * 60 * 24 * 7) {
        that.setData({
          rightBtnHidden: true,
        })
      } else {
        that.setData({
          rightBtnHidden: false,
        })
      }
    }
  },
  judgeReundData() {
    // var that = this;
    // var time = timeChange.formatTime(that.data.startTime);
    // console.log(time)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearTimeout(this.data.timer)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})